
package com.jf.cloud.distribution.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.jf.cloud.api.feign.SearchSpuFeignClient;
import com.jf.cloud.api.platform.feign.ConfigFeignClient;
import com.jf.cloud.api.platform.vo.DistributionConfigApiVO;
import com.jf.cloud.api.platform.vo.DistributionConfigSpuVO;
import com.jf.cloud.api.platform.vo.DistributionRecruitConfigApiVO;
import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.product.vo.search.SpuSearchVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.util.Json;
import com.jf.cloud.distribution.service.DistributionConfigService;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zz
 * @date 2021-08-12 10:06:38
 */
@Service
public class DistributionConfigServiceImpl implements DistributionConfigService {

    @Autowired
    private ConfigFeignClient configFeignClient;

    @Autowired
    private SearchSpuFeignClient searchSpuFeignClient;

    @Autowired
    private MapperFacade mapperFacade;

    @Override
    public DistributionConfigApiVO getDistributionConfig() {
        ServerResponseEntity<String> config = configFeignClient.getConfig(Constant.DISTRIBUTION_CONFIG);
        if (!config.isSuccess()) {
            throw new LuckException(config.getMsg());
        }
        String value = config.getData();
        if (StrUtil.isBlank(value)) {
            throw new LuckException("分销基本设置没有配置");
        }
        DistributionConfigApiVO distributionConfigVO = Json.parseObject(value, DistributionConfigApiVO.class);
        List<Long> spuIdList = distributionConfigVO.getSpuIdList();
        if (CollUtil.isEmpty(spuIdList)) {
            return distributionConfigVO;
        }
        ServerResponseEntity<List<SpuSearchVO>> spuResponse = searchSpuFeignClient.listSpuBySpuIds(spuIdList);
        if (spuResponse.isSuccess() && CollUtil.isNotEmpty(spuResponse.getData())) {
            distributionConfigVO.setSpuList(mapperFacade.mapAsList(spuResponse.getData(), DistributionConfigSpuVO.class));
        }
        return distributionConfigVO;
    }

    @Override
    public DistributionRecruitConfigApiVO getDistributionRecruitConfig() {
        ServerResponseEntity<String> config = configFeignClient.getConfig(Constant.DISTRIBUTION_RECRUIT_CONFIG);
        if (!config.isSuccess()) {
            throw new LuckException(config.getMsg());
        }
        String value = config.getData();
        if (StrUtil.isBlank(value)) {
            throw new LuckException("分销推广设置没有配置");
        }
        return Json.parseObject(value, DistributionRecruitConfigApiVO.class);
    }
}
